*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	Determines the set of students with risk at at least one school, storing the
*	sample indicator as lottery_*_sample. Also returns a matrix G with the
*	number of students at risk at each school.
*	----------------------------------------------------------------------------

	program define sharpsample, rclass
	cap program drop sharpsample
	syntax [if] [in], [lotteries(numlist)] ptype(name) [bin] [goodcell] [minrisk(integer 1)]

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	marksample touse

	if "`bin'" == "" & "`lotteries'" == ""{
		di "sharpsample: need to specify lotteries if not binning"
		error 1
	}

	//drop existing sample vars
	if "`bin'" != "" local bin bin
	if "`bin'" == ""{
		cap drop lottery_`ptype'_sch
		if _rc == 0 di "Replacing existing lottery sch indicator"
		gen lottery_`ptype'_sch = 0
	}
	cap drop `bin'lottery_`ptype'_sample
	if _rc == 0 di "Replacing existing sample vars"

	//helper locals
	local type = subinstr("`ptype'","i","",1)
	local risklist
	local rows
	local j 0
	local n 1

	//count number of lotteries
	if "`bin'"=="" local J: word count `lotteries'
	else{
		unab all: binpscore_`type'_*
		cap unab zeros: binpscore_`type'_*_0
		local binpscores: list all - zeros
		local J: word count `binpscores'
	}

	mat G=J(`J',1,.)

	if "`bin'"==""{
		foreach sch in `lotteries'{
			cap confirm variable pscore_`type'_`sch'
			if _rc==0{
				tempvar sigma risk_`type'_`sch'

				if strpos("`ptype'","i") | "`goodcell'"!= ""{
					qui bys year pscore_`type'_`sch': egen `sigma' = sd(offer_`sch') if `touse'
					qui gen `risk_`type'_`sch'' = !inlist(pscore_`type'_`sch',0,1,.) & (`sigma'>0) & !mi(`sigma') & `touse'
				}
				else qui gen `risk_`type'_`sch'' = !inlist(pscore_`type'_`sch',0,1,.) & `touse'

				qui count if `risk_`type'_`sch'' == 1
				mat G[`n',1] = r(N)
				qui replace lottery_`ptype'_sch = 1 if sch == `sch' & r(N)>=`minrisk'
				if r(N)>=`minrisk' local risklist `risklist' `risk_`type'_`sch''
				local ++j
			}
			else mat G[`n',1] = 0
			local rows `rows' `sch'
			local ++n
		}
	}
	else{
		foreach binpscore in `binpscores'{
			local num = subinstr("`binpscore'","binpscore_`type'_","",1)

			tempvar sigma binrisk_`type'_`num'
			if strpos("`ptype'","i") | "`goodcell'"!= ""{
				qui bys year `binpscore': egen `sigma' = sd(binoffer_`num') if `touse'
				qui gen `binrisk_`type'_`num'' = !inlist(`binpscore',0,1,.) & (`sigma'>0) & !mi(`sigma') & `touse'
			}
			else qui gen `binrisk_`type'_`num'' = !inlist(`binpscore',0,1,.) & `touse'

			qui count if `binrisk_`type'_`num'' == 1
			mat G[`n++',1] = r(N)
			local risklist `risklist' `binrisk_`type'_`num''
			local rows `rows' bin_`num'
			local ++j
		}
	}

	mat rownames G = `rows'
	return matrix G = G

	//define lottery sample
	egen `bin'lottery_`type'_sample = rowmax(`risklist')
	di "sharpsample: `bin'lottery_`type'_sample generated based on `j' (found out of `J') pscore variables"

	end
